function [A] = estimateA(input,b)
%Author: Wang GuangYuan
%ESTIMATEA 
% when a white object exists
% in the scene, if the window selection during the dark-channel
% image acquisition is not appropriate, the white object will
% remain and cause the selected atmospheric light intensity to
% be based predominantly on this object.
% In outdoor images, the sky portion generally has the
% characteristics of large area, bright color and smoothness.
% Therefore, to nd pixels with high brightness and smoothness,
% the gray projection method is used for location isolation.
if(~exist('b','var'))
    b=2;
end
input=double(input)./255;
for c=1:3
    channel=input(:,:,c);
    H1=sum(channel);
    for pos=1:length(H1)
        H_MAX(pos)=H1(pos);
        num=1;
        for diff=1:b
            if(pos-diff<1)
               %donothing
            else
                H_MAX(pos)=H_MAX(pos)+H1(pos-diff);
                num=num+1;
            end
           if(pos+diff>length(H1)) 
               %donothing
           else
               H_MAX(pos)=H_MAX(pos)+H1(pos+diff);
               num=num+1;
           end   
        end
        H_MAX(pos)=H_MAX(pos)/num;
    end
      pos=find(H_MAX==max(H_MAX)); 
      low_y=pos-diff;
      high_y=pos+diff;

      if(low_y<1)
          low_y=1;
      end
      if(high_y>length(H1))
          high_y=length(H1);
      end

      candidate_region=channel(:,low_y:high_y);
      topDark=sort(candidate_region(:),'descend');
      idx=round(0.01*length(candidate_region(:)));
      A(c)=mean(topDark(1:idx))*255; 
end
        

end

